from bs4 import BeautifulSoup
import requests
import json
from GKData import GKData
from MyEncoder import MyEncoder

base_url = 'https://www.alter-web.jp'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}

all_datas = []

count = 0


def start():
    for year in range(2005, 2025):
        for month in range(1, 13):
            try:
                response = requests.get(f"{base_url}/products/?yy={year}&mm={month}", headers=headers)
                print(f'url={base_url}/products/?yy={year}&mm={month}')
                soup = BeautifulSoup(response.content, 'html.parser')
                a = soup.find('div', class_='page type-a').find('figure').find('a')
                parseUrl(a['href'])
            except Exception as e:
                pass
    writeToFile(toJson(all_datas), 'Alter')


def parseUrl(url):
    gk_data = GKData()
    response = requests.get(f"{base_url}{url}", headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    list = soup.findAll('div', class_='inner sitewidth')
    # 名称
    name = list[0].find('div', class_='module').find('h1').text
    gk_data.name = name
    images = []
    imgs = list[1].find('div', class_='module').find('div', class_='imgset').findAll('img')
    for img in imgs:
        images.append(f"{base_url}{img['src']}")
    gk_data.images = images

    cells = soup.find('div', class_='cells col-02').findAll('div', class_='cell')
    for cell in cells:
        trs = cell.find('table').find('tbody').findAll('tr')
        for tr in trs:
            th = tr.find('th')
            if th.text.strip() == '作品名':
                gk_data.productName = tr.find('td').text
            if th.text.strip() == 'Title/Name':
                gk_data.title = tr.find('td').text
            if th.text.strip() == '発売月':
                gk_data.subscribeAt = tr.find('td').text
            if th.text.strip() == '価格':
                gk_data.price = tr.find('td').text
            if th.text.strip() == 'サイズ':
                gk_data.size = tr.find('td').text
            if th.text.strip() == '原 型':
                gk_data.proto = tr.find('td').text
            if th.text.strip() == '彩 色':
                gk_data.color = tr.find('td').text
    all_datas.append(gk_data)
    print(f"当前共 {len(all_datas)}个 商品")


def writeToFile(data, fileName):
    with open(f'./data/{fileName}.json', 'a') as file:
        file.write(data)


def toJson(obj):
    return json.dumps(obj, cls=MyEncoder)


start()
